//
//  NumberMenuView.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/5.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "NumberMenuView.h"

@implementation NumberMenuView

- (void)didClickButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(numberMenuViewSelectedIndex:)])
    {
        [self.delegate numberMenuViewSelectedIndex:sender.tag];
    }
    [self didClickButtonUp:sender];
}

- (void)didClickButtonDown:(UIButton *)sender
{
    switch (sender.tag) {
        case 62:
            _aimingCircleView.image = [UIImage imageNamed:@"aiming_circle_up"];
            break;
        case 82:
            _aimingCircleView.image = [UIImage imageNamed:@"aiming_circle_down"];
            break;
        case 71:
            _aimingCircleView.image = [UIImage imageNamed:@"aiming_circle_left"];
            break;
        case 72:
            _aimingCircleView.image = [UIImage imageNamed:@"queding_pre"];
            break;
        case 73:
            _aimingCircleView.image = [UIImage imageNamed:@"aiming_circle_right"];
            break;
        case 61:
            _volumeView.image = [UIImage imageNamed:@"volume_bg_up"];
            break;
        case 81:
            _volumeView.image = [UIImage imageNamed:@"volume_bg_down"];
            break;
        case 63:
            _programView.image = [UIImage imageNamed:@"program_bg_up"];
            break;
        case 83:
            _programView.image = [UIImage imageNamed:@"program_bg_down"];
            break;

        default:
            break;
    }
}

- (void)didClickButtonUp:(UIButton *)sender
{
    switch (sender.tag) {
        case 62:
        case 82:
        case 71:
        case 72:
        case 73:
            _aimingCircleView.image = [UIImage imageNamed:@"aiming_circle"];
            break;
        case 61:
        case 81:
            _volumeView.image = [UIImage imageNamed:@"volume_bg"];
            break;
        case 63:
        case 83:
            _programView.image = [UIImage imageNamed:@"program_bg"];
            break;
            
        default:
            break;
    }
}

- (UIImageView *)aimingCircleView
{
    if(!_aimingCircleView)
    {
        UIView *superView = self;
        _aimingCircleView = [[UIImageView alloc]init];
        [superView addSubview:_aimingCircleView];
        _aimingCircleView.image = [UIImage imageNamed:@"aiming_circle"];
    }
    return _aimingCircleView;
}

- (UIImageView *)programView
{
    if(!_programView)
    {
        UIView *superView = self;
        _programView = [[UIImageView alloc]init];
        [superView addSubview:_programView];
        _programView.image = [UIImage imageNamed:@"program_bg"];
    }
    return _programView;
}
- (UIImageView *)volumeView
{
    if(!_volumeView)
    {
        UIView *superView = self;
        _volumeView = [[UIImageView alloc]init];
        [superView addSubview:_volumeView];
        _volumeView.image = [UIImage imageNamed:@"volume_bg"];
    }
    return _volumeView;
}


- (UIButton *)volumeUpButton
{
    if(!_volumeUpButton)
    {
        UIView *superView = self;
        _volumeUpButton = [[UIButton alloc]init];
        [superView addSubview:_volumeUpButton];
        _volumeUpButton.tag = 61;
        [_volumeUpButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_volumeUpButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_volumeUpButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _volumeUpButton;
}
- (UIButton *)volumeDownButton
{
    if(!_volumeDownButton)
    {
        UIView *superView = self;
        _volumeDownButton = [[UIButton alloc]init];
        [superView addSubview:_volumeDownButton];
        _volumeDownButton.tag = 81;
        [_volumeDownButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_volumeDownButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_volumeDownButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _volumeDownButton;
}
- (UIButton *)programUpButton
{
    if(!_programUpButton)
    {
        UIView *superView = self;
        _programUpButton = [[UIButton alloc]init];
        [superView addSubview:_programUpButton];
        _programUpButton.tag = 63;
        [_programUpButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_programUpButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_programUpButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _programUpButton;
}
- (UIButton *)programDownButton
{
    if(!_programDownButton)
    {
        UIView *superView = self;
        _programDownButton = [[UIButton alloc]init];
        [superView addSubview:_programDownButton];
        _programDownButton.tag = 83;
        [_programDownButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_programDownButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_programDownButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _programDownButton;
}
- (UIButton *)upButton
{
    if(!_upButton)
    {
        UIView *superView = self;
        _upButton = [[UIButton alloc]init];
        [superView addSubview:_upButton];
        _upButton.tag = 62;
        [_upButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_upButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_upButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpOutside];
    }
    return _upButton;
}
- (UIButton *)downButton
{
    if(!_downButton)
    {
        UIView *superView = self;
        _downButton = [[UIButton alloc]init];
        [superView addSubview:_downButton];
        _downButton.tag = 82;
        [_downButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_downButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_downButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpOutside];
    }
    return _downButton;
}
- (UIButton *)leftButton
{
    if(!_leftButton)
    {
        UIView *superView = self;
        _leftButton = [[UIButton alloc]init];
        [superView addSubview:_leftButton];
        _leftButton.tag = 71;
        [_leftButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_leftButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_leftButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpOutside];
    }
    return _leftButton;
}
- (UIButton *)rightButton
{
    if(!_rightButton)
    {
        UIView *superView = self;
        _rightButton = [[UIButton alloc]init];
        [superView addSubview:_rightButton];
        _rightButton.tag = 73;
        [_rightButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_rightButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_rightButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpOutside];
    }
    return _rightButton;
}
- (UIButton *)confirmButton
{
    if(!_confirmButton)
    {
        UIView *superView = self;
        _confirmButton = [[UIButton alloc]init];
        [superView addSubview:_confirmButton];
        _confirmButton.tag = 72;
        [_confirmButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_confirmButton addTarget:self action:@selector(didClickButtonDown:) forControlEvents:UIControlEventTouchDown];
        [_confirmButton addTarget:self action:@selector(didClickButtonUp:) forControlEvents:UIControlEventTouchUpOutside];
    }
    return _confirmButton;
}



- (void)masLayoutSubview
{
    __weak UIView *superView = self;
    
    NSArray *array = @[@[@"",@"",@""],
                       @[@"1",@"2",@"3"],
                       @[@"4",@"5",@"6"],
                       @[@"7",@"8",@"9"],
                       @[@"菜单",@"0",@"返回"]];
    
    UIView *lastView;
    [superView addSubview:lastView];
    for (int i = 0; i < 5; i++)      // 行
    {
        for (int j = 0; j < 3; j++)    // 列
        {
            
            UIButton *button = [[UIButton alloc] init];
            [superView addSubview:button];
//            [button setBackgroundColor:rgba(65,98,170,1)];
            [button setBackgroundImage:imageWithName(@"number_button_bg") forState:UIControlStateNormal];
            [button setTitle:array[i][j] forState:UIControlStateNormal];
            [button.titleLabel setFont:fontCommonSize];

            if (i == 0)
            {
                if (j == 0)
                {
                    [button setBackgroundImage:imageWithName(@"icon_power_bg") forState:UIControlStateNormal];
//                    [button setImage:imageWithName(@"icon_power") forState:UIControlStateNormal];
//                    [button setBackgroundColor:rgba(251,74,79,1)];
                }
                else if (j == 1)
                {
//                    [button setBackgroundColor:[UIColor clearColor]];
                    button.hidden = YES;
                    button.userInteractionEnabled = NO;
                }
                else if (j == 2)
                {
                    [button setImage:imageWithName(@"icon_mute") forState:UIControlStateNormal];
                }
            }
            button.layer.cornerRadius = 3;
            button.tag = (i+1)*10+j+1;
            [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i == 0)
                {
                    make.top.mas_equalTo(superView);
                }
                else if (j == 0)
                {
                    make.top.mas_equalTo(lastView.mas_bottom).with.offset(px1920Height(70));
                }
                else
                {
                    make.top.mas_equalTo(lastView);
                }
                
                if (j == 0)
                {
                    make.left.mas_equalTo(superView.mas_left);
                }
                else
                {
                    make.left.mas_equalTo(lastView.mas_right).with.offset(px1080Width(90));
                }
                
                make.height.mas_equalTo(px1920Height(113));
                make.width.mas_equalTo(px1080Width(240));
                
            }];
            
            lastView = button;
        }
    }
    
    [self.aimingCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lastView.mas_bottom).with.offset(px1920Height(100));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.height.width.mas_equalTo(px1920Height(500));
    }];
    
    [self.volumeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.aimingCircleView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(20));
        make.height.mas_equalTo(px1920Height(400));
        make.width.mas_equalTo(px1080Width(80));
    }];
    
    [self.programView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.aimingCircleView.mas_centerY);
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(20));
        make.height.mas_equalTo(px1920Height(362));
        make.width.mas_equalTo(px1080Width(80));
    }];
    
    [self.volumeUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_volumeView);
        make.left.mas_equalTo(_volumeView.mas_left).with.offset(-px1080Width(20));
        make.right.mas_equalTo(_volumeView.mas_right).with.offset(px1080Width(20));
        make.bottom.mas_equalTo(_volumeView.mas_centerY);
    }];

    [self.volumeDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_volumeView);
        make.left.right.mas_equalTo(_volumeUpButton);
        make.top.mas_equalTo(_volumeView.mas_centerY);
    }];

    [self.programUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_programView);
        make.left.mas_equalTo(_programView.mas_left).with.offset(-px1080Width(20));
        make.right.mas_equalTo(_programView.mas_right).with.offset(px1080Width(20));
        make.bottom.mas_equalTo(_programView.mas_centerY);
    }];

    [self.programDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_programView);
        make.left.mas_equalTo(_programView.mas_left).with.offset(-px1080Width(20));
        make.right.mas_equalTo(_programView.mas_right).with.offset(px1080Width(20));
        make.top.mas_equalTo(_programView.mas_centerY);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.aimingCircleView);
        make.height.width.mas_equalTo(px1920Height(250));
    }];
    
    [self.upButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.confirmButton.mas_top);
        make.centerX.mas_equalTo(self.confirmButton.mas_centerX);
        make.height.width.mas_equalTo(self.confirmButton);
    }];

    [self.downButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.confirmButton.mas_bottom);
        make.centerX.mas_equalTo(self.confirmButton.mas_centerX);
        make.height.width.mas_equalTo(self.confirmButton);
    }];

    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.confirmButton.mas_left);
        make.centerY.mas_equalTo(self.confirmButton.mas_centerY);
        make.height.width.mas_equalTo(self.confirmButton);
    }];

    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.confirmButton.mas_right);
        make.centerY.mas_equalTo(self.confirmButton.mas_centerY);
        make.height.width.mas_equalTo(self.confirmButton);
    }];
    
}

@end
